package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.photoeditor.R;
import com.photoeditor.demo.model.bean.BitmapBean;
import com.photoeditor.demo.model.image.collage.templet.CollageTemplet;
import com.photoeditor.demo.model.image.collage.templet.ITempletChangeListener;
import com.photoeditor.demo.model.image.collage.templet.Ratio;
import com.photoeditor.demo.ui.activity.edit.EditorImageActivity;
import com.photoeditor.demo.ui.widget.HorizontalListView;

import java.util.ArrayList;

public class MirrorBarView extends RelativeLayout {

    private HorizontalListView mHorizontalListView;

    private MirrorListAdapter mTemplateAdapter;

    private ITempletChangeListener mTempletChangeListener;

    private EditorImageActivity mActivity;

    private Bitmap mBaseBitmap;
    private BitmapBean mBitmapBean;

    public MirrorBarView(Context context) {
        this(context, null);
    }

    public MirrorBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MirrorBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mActivity = (EditorImageActivity) getContext();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mHorizontalListView = (HorizontalListView) findViewById(R.id.mirror_template_list);
    }

    public void init(ArrayList<CollageTemplet> data, Ratio.RATIO type, ITempletChangeListener templetChangeListener) {
        mTempletChangeListener = templetChangeListener;
        mTemplateAdapter = new MirrorListAdapter(getContext(), data, type);
        if (mBaseBitmap != null) {
            mTemplateAdapter.setBaseBitmap(mBaseBitmap, mBitmapBean);
        }
        mHorizontalListView.setAdapter(mTemplateAdapter);
        mHorizontalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mTemplateAdapter.setSelectedPosition(position, view);
                if (mTempletChangeListener != null) {
                    mTempletChangeListener.onTempletChange(mTemplateAdapter.getItem(position));
                }
            }
        });
    }

    public void reset() {
        mTemplateAdapter.setSelectedPosition(0);
    }

    /**
     * @param bitmap
     */
    public void setBaseBitmap(Bitmap bitmap, BitmapBean bean) {
        mBaseBitmap = bitmap;
        mBitmapBean = bean;
        if (mTemplateAdapter != null) {
            mTemplateAdapter.setBaseBitmap(bitmap, bean);
        }
    }

    public Bitmap getBaseBitmap() {
        return mBaseBitmap;
    }
}
